<?php
$db = new Database;
$auth = Auth::getAuth('current_user');
$user_id = $auth['id'];
$company_id = $auth['company_id'];
if(isset($_POST['action'])){
    $userID = $_POST['userID'];
    if(!empty($userID)){
        if($_POST['action']=="acceptUser"){
            $role = $_POST['roleToInsert'];
            $roleType = $_POST['roleTypeToInsert'];
            $position = $_POST['positionToInsert'];
            $update = array(
                "status"=>1,
                "role"=>$role,
                "role_type"=>$roleType,
                "position_id"=>$position
                );
            $condition = array(
                "id"=>$userID
                );
            $db->update("tbuser",$update,$condition);
            echo "accepted";
        }else if($_POST['action']=="acceptManyUser"){
            $roleToInsert = explode(",",implode(",",$_POST['roleToInsert']));
            $roleTypeToInsert = explode(",",implode(",",$_POST['roleTypeToInsert']));
            $positionToInsert = explode(",",implode(",",$_POST['positionToInsert']));
            $userid_array = explode(",",implode(",",$_POST['userID']));
            for($a=0;$a<count($roleToInsert);$a++){
                //echo $roleToInsert[$a]." -> ".$roleTypeToInsert[$a]." -> ".$positionToInsert[$a]." -> ".$userid_array[$a]."\n";
                $update = array(
                    "status"=>1,
                    "role"=>$roleToInsert[$a],
                    "role_type"=>$roleTypeToInsert[$a],
                    "position_id"=>$positionToInsert[$a]
                    );
                $condition = array(
                    "id"=>$userid_array[$a]
                    );
                $db->update("tbuser",$update,$condition);
            }
            echo "accepted";
        }else if($_POST['action']=="deleteUser"){
            $condition = array(
                "id"=>$userID
            );
            $db->delete("tbuser",$condition);
            echo "deleteUser";
        }else if($_POST['action']=="restoreUser"){
            $getPendingRequests = $db->query("SELECT * FROM tbrequest where user_id = $userID and company_id='$company_id' and status=3","array");
            foreach($getPendingRequests as $setPendingRequests){
                $getRProcess = $db->query("SELECT * FROM tbrequest_process where request_id = '".$setPendingRequests['id']."' and status=3 and approver='1'","array");
                foreach($getRProcess as $setRProcess){
                    $update = array(
                        "status"=>0,
                        "approver"=>""
                        );
                    $condition = array(
                        "id"=>$setRProcess['id']
                        );
                    $db->update("tbrequest_process",$update,$condition);
                }
                $update = array(
                    "status"=>0
                    );
                $condition = array(
                    "id"=>$setPendingRequests['id']
                    );
                $db->update("tbrequest",$update,$condition);
            }
            $update = array(
                "is_active"=>1
                );
            $condition = array(
                "id"=>$userID
                );
            $db->update("tbuser",$update,$condition);
            echo "restored";
        }else if($_POST['action']=="deactivateUser"){
            $getPendingRequests = $db->query("SELECT * FROM tbrequest where user_id = $userID and company_id='$company_id' and status=0","array");
            foreach($getPendingRequests as $setPendingRequests){
                $getRProcess = $db->query("SELECT * FROM tbrequest_process where request_id = '".$setPendingRequests['id']."' and status=0","array");
                foreach($getRProcess as $setRProcess){
                    $update = array(
                        "status"=>3,
                        "approver"=>""
                        );
                    $condition = array(
                        "id"=>$setRProcess['id']
                        );
                    $db->update("tbrequest_process",$update,$condition);
                }
                $update = array(
                    "status"=>3
                    );
                $condition = array(
                    "id"=>$setPendingRequests['id']
                    );
                $db->update("tbrequest",$update,$condition);
            }
            $update = array(
                "is_active"=>0
                );
            $condition = array(
                "id"=>$userID
                );
            $db->update("tbuser",$update,$condition);
            echo "deactivated";
            $getUser = $db->query("SELECT * FROM tbuser where id='$userID'","row");
            $userRole = $getUser['role'];
            $userRoleType = $getUser['role_type'];
            
            $getRequest = $db->query("SELECT w.config_status as config_status, r.id as request_id, r.user_id as user_id, w.id as workflow_id, rp.wf_master_id as nextApprover
                                    FROM tbrequest r
                                    LEFT JOIN tbrequest_process rp on r.id=rp.request_id
                                    LEFT JOIN tbform f on f.id=r.form_id
                                    LEFT JOIN tbworkflow w on f.workflow_id=w.id
                                    WHERE (".roleApprover($userRoleType,$userRole)."
                                    or rp.approver='".$auth['firstname']." ".$auth['lastname'] ."')
                                    AND rp.status=0 and r.status=0
                                    and r.company_id='$company_id'","array");
            $configStatusArray = array();
            $request_id = array();
            $requestor = array();
            $diagram_id = array();
            $nextAproverID = array();
            foreach($getRequest as $setRequest){
                array_push($configStatusArray,$setRequest['config_status']);
                array_push($request_id,$setRequest['request_id']);
                array_push($requestor,$setRequest['user_id']);
                array_push($diagram_id,$setRequest['workflow_id']);
                array_push($nextAproverID,$setRequest['nextApprover']);
            }
            updatePendingRequest($userID,$company_id,$configStatusArray,$request_id,$requestor,$diagram_id,$nextAproverID);
        }else if($_POST['action']=="declineUser"){
            $update = array(
                "is_active"=>0
                );
            $condition = array(
                "id"=>$userID
                );
            $db->update("tbuser",$update,$condition);
            echo "declined";
        }else if($_POST['action']=="saveEditUser"){
            $role = $_POST['roleToInsert'];
            $roleType = $_POST['roleTypeToInsert'];
            $position = $_POST['positionToInsertID'];
            $getUser = $db->query("SELECT * FROM tbuser where id = '$userID'","row");
            if($getUser['role']==$role and $getUser['role_type']==$roleType and $getUser['position_id']==$position){
                echo 1;
            }else{
                $getPendingRequests = $db->query("SELECT * FROM tbrequest where user_id = $userID and company_id='$company_id' and status=0","array");
                $getPendingRequestsRows = $db->query("SELECT * FROM tbrequest where user_id = $userID and company_id='$company_id' and status=0","numrows");
                if($getPendingRequestsRows>0){
                    foreach($getPendingRequests as $setPendingRequests){
                        $conditionDel = array(
                            "request_id"=>$setPendingRequests['id']
                            );
                        $db->delete("tbrequest_process",$conditionDel);
                        $getworkflow_id = $db->query("SELECT workflow_id FROM tbform where id = '".$setPendingRequests['form_id']."'","row");
                        $getActiveOrgChart = $db->query("SELECT id FROM tborgchart where is_active = 1 and company_id='$company_id' and status=1","row");
                        $getObjectID = $db->query("SELECT object_id from tborgchartobjects where role='$user_group' and orgChart_id='".$getActiveOrgChart['id']."'","row");
                        $d = 0;
                        $arrayWorkflowRole = array();
                        $arrayWorkflowRoleStatus = array();
                        $arrayOrgchart = array();
                        $arrayWorkflowObjID = array();
                        $push = false;
                        while(true){
                            if($d==0){
                                $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = 'flowspan_1_worklow' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");    
                            }else{
                                $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = '".$master['child_id']."' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");    
                            }
                            $queryMasterNode = $db->query("SELECT * FROM tbworflow_obects where object_id='".$master['child_id']."' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");
                            if($queryMasterNode['role']=="END"){
                                break;
                            }else{
                                //echo $queryMasterNode['role']."\n";
                                if($queryMasterNode['role']=="Head" || $queryMasterNode['role']=="Assistant Head"){
                                    //for the the submitter
                                    if($d==0 and $roleType!=1){ //hindi head
                                        $dept = $role;
                                        $masterOrgChart = $db->query("SELECT object_id FROM tborgchartobjects where role='$role' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                    }else{
                                        if($d==0){
                                            $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where child_id = '".$getObjectID['object_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                                        }else{
                                            $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where (child_id = '".$masterOrgChart['master_id']."' || child_id='".$masterOrgChart['object_id']."') and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                        }
                                        $queryMasterNodeOrgChart = $db->query("SELECT * FROM tborgchartobjects where object_id='".$masterOrgChart['master_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                        $dept = $queryMasterNodeOrgChart['role'];
                                        
                                    }
                                    //echo $queryMasterNode['role']."_".$dept."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                                }else{
                                    $dept = "";
                                    //echo $queryMasterNode['role']."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                                }
                            }
                            //echo $queryMasterNode['role'].$dept."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                            array_push($arrayWorkflowRole,$queryMasterNode['role']);
                            array_push($arrayWorkflowRoleStatus,$queryMasterNode['status']);
                            array_push($arrayWorkflowObjID,$master['child_id']);
                            array_push($arrayOrgchart,$dept);
                            $d++;
                        }
                        $minus = 0;
                        //temporary get the flow of the approver
                        $roleToInserts = "";$object_id2 = "";$lastApproverCount=0;$getRowsApprover = 0;$confirmation=0;$confirm = 0;$proceedToNext = 0;$approverFlow = "";
                        for($o=0;$o<count($arrayWorkflowRole);$o++){
                            if($arrayWorkflowRole[$o]=="Head" || $arrayWorkflowRole[$o]=="Assistant Head"){
                                if($arrayWorkflowRole[$o]=="Head" and $arrayWorkflowRole[$o-1]=="Assistant Head"){
                                    $minus++;
                                }
                                $roleToInserts = $arrayWorkflowRole[$o]."_".$arrayOrgchart[$o-$minus];
                                $getRowsApprover = $db->query("SELECT * FROM tbuser where role_type='".tempGroupType($arrayWorkflowRole[$o])."' and role='".$arrayOrgchart[$o-$minus]."' and is_active=1 and status =1 and company_id = '$company_id'","numrows");
                                //echo $roleToInserts."->".$getRowsApprover."\n";
                                if($getRowsApprover>0){
                                    $lastApproverCount++;
                                    $approver = $arrayWorkflowRole[$o]."_".$arrayOrgchart[$o-$minus];
                                }
                            }else{
                                $roleToInserts = $arrayWorkflowRole[$o];
                                $getRowsApprover = $db->query("SELECT * FROM tbuser u LEFT JOIN tbposition p on u.position_id = p.id where (concat_ws(' ',u.firstname,u.lastname) = '".$roleToInserts."' or p.position = '".$roleToInserts."') and u.is_active=1 and u.status =1 and u.company_id = $company_id","numrows");
                                //echo $roleToInserts."->".$getRowsApprover."\n";
                                if($getRowsApprover>0){
                                   $approver = $arrayWorkflowRole[$o];
                                   $lastApproverCount++;
                                }
                            }
                            if($o==0 && $getRowsApprover==0){
                                $confirm = 1;
                            }
                            if($getRowsApprover>0){
                                $approverStatus = $arrayWorkflowRoleStatus[$o];
                                $master_id = $arrayWorkflowObjID[$o];
                                $proceedToNext++;
                                //echo $approver."\n";
                                break;
                            }
                        }
                        $insertRequestProcess = array(
                                    "request_id"=>$setPendingRequests['id'],
                                    "approver"=>$approver,
                                    "approverStatus"=>$approverStatus,
                                    "wf_master_id"=>$master_id,
                                    "status"=>0
                            );
                        $requestProcess_id = $db->insert("tbrequest_process",$insertRequestProcess);
                        /*$getworkflow_id = $db->query("SELECT workflow_id FROM tbform where id = '".$setPendingRequests['form_id']."'","row");
                        $getActiveOrgChart = $db->query("SELECT id FROM tborgchart where is_active = 1 and company_id='$company_id' and status=1","row");
                        $getObjectID = $db->query("SELECT object_id from tborgchartobjects where role='$role' and orgChart_id='".$getActiveOrgChart['id']."'","row");
                        $getMasterObjectID = $db->query("SELECT master_id from tborgchartline where child_id='".$getObjectID['object_id']."' and orgChart_id='".$getActiveOrgChart['id']."'","row");
                        $d=0;
                        $lastApproverCount = 0;
                        while(true){
                            if($d==0){
                                $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = 'flowspan_1_worklow' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");    
                            }else{
                                $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = '".$master['child_id']."' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");    
                            }
                            $queryMasterNode = $db->query("SELECT * FROM tbworflow_obects where object_id='".$master['child_id']."' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");
                            if($queryMasterNode['role']=="END"){
                                break;
                            }else{
                                if($queryMasterNode['role']=="Head" || $queryMasterNode['role']=="Assistant Head"){
                                    if($d==0 && $roleType!=1){
                                        $approver = $queryMasterNode['role']."_".$role;
                                        $userRole = $role;
                                        $masterOrgChart = $db->query("SELECT object_id FROM tborgchartobjects where role='$role' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                    }else{
                                        if($d==0){
                                            $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where child_id = '".$getObjectID['object_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                                        }else{
                                            $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where (child_id = '".$masterOrgChart['master_id']."' or child_id='".$masterOrgChart['object_id']."') and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                                        }
                                        $queryMasterNodeOrgChart = $db->query("SELECT * FROM tborgchartobjects where object_id='".$masterOrgChart['master_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                        $approver = $queryMasterNode['role']."_".$queryMasterNodeOrgChart['role'];
                                        $userRole = $queryMasterNodeOrgChart['role'];
                                    }
                                    $getRowsApprover = $db->query("SELECT * FROM tbuser where role_type='".tempGroupType($queryMasterNode['role'])."' and role='$userRole' and is_active=1 and status =1 and id!=$userID","numrows");
                                    if($getRowsApprover>0){
                                        $lastApproverCount++;
                                    }
                                }else{
                                    $getRowsApprover = $db->query("SELECT * FROM tbuser u LEFT JOIN tbposition p on u.position_id = p.id where concat_ws(' ',u.firstname,u.lastname) = '".$queryMasterNode['role']."' or p.position = '".$queryMasterNode['role']."' and u.is_active=1 and u.status =1","numrows");
                                    if($getRowsApprover>0){
                                        $approver = $queryMasterNode['role'];
                                        $lastApproverCount++;
                                    }
                                }
                                if($lastApproverCount>0){
                                    $master_id = $queryMasterNode['object_id'];
                                    $approverStatus = $queryMasterNode['status'];
                                    //echo $approver."->".$master_id."->".$approverStatus;
                                    $insertRequestProcess = array(
                                                "request_id"=>$setPendingRequests['id'],
                                                "approver"=>$approver,
                                                "approverStatus"=>$approverStatus,
                                                "wf_master_id"=>$master_id,
                                                "status"=>0
                                        );
                                    //$requestProcess_id = $db->insert("tbrequest_process",$insertRequestProcess);
                                    break;
                                }
                            }
                            $d++;
                        }
                        */
                        if($lastApproverCount==0){
                            echo " Last Processor (No next Processor)";
                        }
                    }
                }else{
                    echo "++";
                }
                $getUser = $db->query("SELECT * FROM tbuser where id='$userID'","row");
                $userRole = $getUser['role'];
                $userRoleType = $getUser['role_type'];
                
                $getRequest = $db->query("SELECT w.config_status as config_status, r.id as request_id, r.user_id as user_id, w.id as workflow_id, rp.wf_master_id as nextApprover
                                        FROM tbrequest r
                                        LEFT JOIN tbrequest_process rp on r.id=rp.request_id
                                        LEFT JOIN tbform f on f.id=r.form_id
                                        LEFT JOIN tbworkflow w on f.workflow_id=w.id
                                        WHERE (".roleApprover($userRoleType,$userRole)."
                                        or rp.approver='".$auth['firstname']." ".$auth['lastname'] ."')
                                        AND rp.status=0 and r.status=0
                                        and r.company_id='$company_id'","array");
                $configStatusArray = array();
                $request_id = array();
                $requestor = array();
                $diagram_id = array();
                $nextAproverID = array();
                foreach($getRequest as $setRequest){
                    array_push($configStatusArray,$setRequest['config_status']);
                    array_push($request_id,$setRequest['request_id']);
                    array_push($requestor,$setRequest['user_id']);
                    array_push($diagram_id,$setRequest['workflow_id']);
                    array_push($nextAproverID,$setRequest['nextApprover']);
                }
                updatePendingRequest($userID,$company_id,$configStatusArray,$request_id,$requestor,$diagram_id,$nextAproverID);
                $update = array(
                    "role"=>$role,
                    "role_type"=>$roleType,
                    "position_id"=>$position
                    );
                $condition = array(
                    "id"=>$userID
                    );
                $db->update("tbuser",$update,$condition);
                //echo "Update Done";
            }
        }else if($_POST['action']=="checkRole"){
            $checkRole = $db->query("SELECT role FROM tbuser where id = '$userID'","row");
            echo $checkRole['role'];
        }else if($_POST['action']=="addRole"){
            $role = $_POST['role'];
            $countRoleExist = $db->query("SELECT * from tbrole where role = '$role' and company_id = '$company_id'","numrows");
            if($countRoleExist==0){
                $roleInsert = array(
                  "role"=>$role,
                  "company_id"=>$company_id,
                  "is_active"=>1
                );
                $db->insert("tbrole",$roleInsert);
            }else{
                echo "roleExist";
            }
        }else if($_POST['action']=="saveEditFloatingUser"){
            $updatedRole = $_POST['roleToUpdate'];
            $update = array(
                "role"=>$updatedRole,
                );
            $condition = array(
                "id"=>$userID
                );
            $db->update("tbuser",$update,$condition);
            echo 1;
        }
    }else{
        echo "Error";
    }
}

function updatePendingRequest($userID,$company_id,$configStatusArray=array(),$request_id=array(),$requestor=array(),$diagram_id=array(),$nextAproverID=array()){
    $db = new Database;
    if(count($request_id)>0){
        for($a=0;$a<count($configStatusArray);$a++){
            $getActiveOrgChart = $db->query("SELECT id FROM tborgchart where is_active = 1 and company_id='$company_id' and status=1","row");
            if($configStatusArray[$a]==1){
                $getRequestor = $db->query("SELECT * FROM tbuser where id='".$requestor[$a]."'","row");
                $user_group = $getRequestor['role'];
                $user_groupType = $getRequestor['role_type'];
                $getObjectID = $db->query("SELECT object_id from tborgchartobjects where role='$user_group' and orgChart_id='".$getActiveOrgChart['id']."'","row");
                $d = 0;
                $arrayWorkflowRole = array();
                $arrayWorkflowRoleStatus = array();
                $arrayOrgchart = array();
                $arrayWorkflowObjID = array();
                $push = false;
                while(true){
                    if($d==0){
                        $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = 'flowspan_1_worklow' and worflow_id = '$diagram_id[$a]'","row");    
                    }else{
                        $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = '".$master['child_id']."' and worflow_id = '$diagram_id[$a]'","row");    
                    }
                    $queryMasterNode = $db->query("SELECT * FROM tbworflow_obects where object_id='".$master['child_id']."' and worflow_id = '$diagram_id[$a]'","row");
                    if($queryMasterNode['role']=="END"){
                        break;
                    }else{
                        //echo $queryMasterNode['role']."\n";
                        if($queryMasterNode['role']=="Head" || $queryMasterNode['role']=="Assistant Head"){
                            //for the the submitter
                            if($d==0 and $user_groupType!=1){ //hindi head
                                $dept = $user_group;
                                $masterOrgChart = $db->query("SELECT object_id FROM tborgchartobjects where role='$user_group' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                            }else{
                                if($d==0){
                                    $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where child_id = '".$getObjectID['object_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                                }else{
                                    $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where (child_id = '".$masterOrgChart['master_id']."' || child_id='".$masterOrgChart['object_id']."') and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                }
                                $queryMasterNodeOrgChart = $db->query("SELECT * FROM tborgchartobjects where object_id='".$masterOrgChart['master_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                $dept = $queryMasterNodeOrgChart['role'];
                                
                            }
                            //echo $queryMasterNode['role']."_".$dept."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                        }else{
                            $dept = "";
                            //echo $queryMasterNode['role']."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                        }
                    }
                    //echo $queryMasterNode['role'].$dept."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                    array_push($arrayWorkflowRole,$queryMasterNode['role']);
                    array_push($arrayWorkflowRoleStatus,$queryMasterNode['status']);
                    array_push($arrayWorkflowObjID,$master['child_id']);
                    array_push($arrayOrgchart,$dept);
                    $d++;
                }
                $minus = 0;
                //temporary get the flow of the approver
                $roleToInserts = "";$object_id2 = "";$lastApproverCount=0;$getRowsApprover = 0;$confirmation=0;$confirm = 0;$proceedToNext = 0;$approverFlow = "";
                for($o=0;$o<count($arrayWorkflowRole);$o++){
                    if($arrayWorkflowRole[$o]=="Head" || $arrayWorkflowRole[$o]=="Assistant Head"){
                        if($arrayWorkflowRole[$o]=="Head" and $arrayWorkflowRole[$o-1]=="Assistant Head"){
                            $minus++;
                        }
                        $roleToInserts = $arrayWorkflowRole[$o]."_".$arrayOrgchart[$o-$minus];
                        $getRowsApprover = $db->query("SELECT * FROM tbuser where role_type='".tempGroupType($arrayWorkflowRole[$o])."' and role='".$arrayOrgchart[$o-$minus]."' and is_active=1 and status =1 and company_id = '$company_id' and id!=$userID","numrows");
                        //echo $roleToInserts."->".$getRowsApprover."\n";
                        if($getRowsApprover>0){
                            $lastApproverCount++;
                            $approver = $arrayWorkflowRole[$o]."_".$arrayOrgchart[$o-$minus];
                        }
                    }else{
                        $roleToInserts = $arrayWorkflowRole[$o];
                        $getRowsApprover = $db->query("SELECT * FROM tbuser u LEFT JOIN tbposition p on u.position_id = p.id where (concat_ws(' ',u.firstname,u.lastname) = '".$roleToInserts."' or p.position = '".$roleToInserts."') and u.is_active=1 and u.status =1 and u.company_id = $company_id and u.id!=$userID","numrows");
                        //echo $roleToInserts."->".$getRowsApprover."\n";
                        if($getRowsApprover>0){
                           $approver = $arrayWorkflowRole[$o];
                           $lastApproverCount++;
                        }
                    }
                    if($o==0 && $getRowsApprover==0){
                        $confirm = 1;
                    }
                    if($getRowsApprover>0){
                        $approverStatus = $arrayWorkflowRoleStatus[$o];
                        $master_id = $arrayWorkflowObjID[$o];
                        $proceedToNext++;
                        //echo $request_id[$a]." ".$approver."\n";
                        break;
                    }
                }
                //echo  "back to the first ung request_id #".$request_id[$a]." ni user id#".$requestor[$a]."\n";
                /*$getRequestor = $db->query("SELECT * FROM tbuser where id='".$requestor[$a]."'","row");
                $getObjectID = $db->query("SELECT object_id from tborgchartobjects where role='".$getRequestor['role']."' and orgChart_id='".$getActiveOrgChart['id']."'","row");
                $d = 0;
                $lastApproverCount = 0;
                while(true){
                    if($d==0){
                        $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = 'flowspan_1_worklow' and worflow_id = '$diagram_id[$a]'","row");    
                    }else{
                        $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = '".$master['child_id']."' and worflow_id = '$diagram_id[$a]'","row");    
                    }
                    $queryMasterNode = $db->query("SELECT * FROM tbworflow_obects where object_id='".$master['child_id']."' and worflow_id = '$diagram_id[$a]'","row");
                    if($queryMasterNode['role']=="END"){
                        break;
                    }else{
                        if($queryMasterNode['role']=="Head" || $queryMasterNode['role']=="Assistant Head"){
                            //FOR HEAD OR ASSISTANT
                            if($d==0 && $getRequestor['role_type']!=1 && $queryMasterNode['role']=="Head"){
                                $userRole = $getRequestor['role'];
                                $approver =  $queryMasterNode['role']."_".$getRequestor['role'];
                                $masterOrgChart = $db->query("SELECT object_id FROM tborgchartobjects where role='".$getRequestor['role']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                            }else{
                                if($d==0){
                                    $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where child_id = '".$getObjectID['object_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                                }else{
                                    $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where (child_id = '".$masterOrgChart['master_id']."' || child_id='".$masterOrgChart['object_id']."') and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                                }
                                $queryMasterNodeOrgChart = $db->query("SELECT * FROM tborgchartobjects where object_id='".$masterOrgChart['master_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                $approver =  $queryMasterNode['role']."_".$queryMasterNodeOrgChart['role'];
                                $userRole = $queryMasterNodeOrgChart['role'];
                            }
                            $getRowsApprover = $db->query("SELECT * FROM tbuser where role_type='".tempGroupType($queryMasterNode['role'])."' and role='$userRole' and is_active=1 and status =1 and id!=$userID","numrows");;
                            if($getRowsApprover>0){
                                $lastApproverCount++;
                            }
                        }else{
                            //FOR SPECIFIC USER OR POSITION
                            $getRowsApprover = $db->query("SELECT * FROM tbuser u LEFT JOIN tbposition p on u.position_id = p.id where concat_ws(' ',u.firstname,u.lastname) = '".$queryMasterNode['role']."' or p.position = '".$queryMasterNode['role']."' and u.is_active=1 and u.status =1 and u.id!=$userID","numrows");
                            if($getRowsApprover>0){
                                $approver = $queryMasterNode['role'];
                                $lastApproverCount++;
                            }
                        }
                        if($lastApproverCount>0){
                            $master_id = $queryMasterNode['object_id'];
                            $approverStatus = $queryMasterNode['status'];
                            echo $approver."->".$master_id."->".$approverStatus."!";
                            break;
                        }
                    }
                    $d++;
                }*/
                if($lastApproverCount>0){
                    //for($newReq=0;$newReq<count($request_id);$newReq++){
                        
                    //}
                    $deleteOldReq = array(
                        "request_id"=>$request_id[$a]
                        );
                    $db->delete("tbrequest_process",$deleteOldReq);
                    $insertRequestProcess = array(
                                "request_id"=>$request_id[$a],
                                "approver"=>$approver,
                                "approverStatus"=>$approverStatus,
                                "wf_master_id"=>$master_id,
                                "status"=>0
                        );
                    $requestProcess_id = $db->insert("tbrequest_process",$insertRequestProcess);
                }else{
                    //echo "Wla ng matitirang processor";
                }
            }else if($configStatusArray[$a]==2){
                //echo "jump to the next ung request_id #".$request_id[$a]." ni user id#".$requestor[$a]."\n";
                $getRequestor = $db->query("SELECT * FROM tbuser where id='".$requestor[$a]."'","row");
                $user_group = $getRequestor['role'];
                $user_groupType = $getRequestor['role_type'];
                $getObjectID = $db->query("SELECT object_id from tborgchartobjects where role='$user_group' and orgChart_id='".$getActiveOrgChart['id']."'","row");
                $d = 0;
                $arrayWorkflowRole = array();
                $arrayWorkflowRoleStatus = array();
                $arrayOrgchart = array();
                $arrayWorkflowObjID = array();
                $push = false;
                while(true){
                    if($d==0){
                        $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = '$nextAproverID[$a]' and worflow_id = '$diagram_id[$a]'","row");    
                    }else{
                        $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = '".$master['child_id']."' and worflow_id = '$diagram_id[$a]'","row");    
                    }
                    $queryMasterNode = $db->query("SELECT * FROM tbworflow_obects where object_id='".$master['child_id']."' and worflow_id = '$diagram_id[$a]'","row");
                    if($queryMasterNode['role']=="END"){
                        break;
                    }else{
                        //echo $queryMasterNode['role']."\n";
                        if($queryMasterNode['role']=="Head" || $queryMasterNode['role']=="Assistant Head"){
                            //for the the submitter
                            $getCurrentApproverRole = $db->query("SELECT * FROM tbuser where id ='$userID'","row");
                            $getObjectIDCurrentApprover = $db->query("SELECT * from tborgchartobjects where role='".$getCurrentApproverRole['role']."' and orgChart_id='".$getActiveOrgChart['id']."'","row");
                            $getNextObjectID = $db->query("SELECT master_id FROM tborgchartline where child_id = '".$getObjectIDCurrentApprover['object_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                            $getNextRole = $db->query("SELECT * FROM tborgchartobjects where object_id='".$masterOrgChart['master_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                            $newDept = "";
                            if($getCurrentApproverRole['role_type']==1){
                                $newDept = $getNextRole['role'];
                            }else{
                                $newDept = $user_group;
                            }
                            if($d==0 and $user_groupType!=1){ //hindi head
                                $dept = $newDept;
                                $masterOrgChart = $db->query("SELECT object_id FROM tborgchartobjects where role='$user_group' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                            }else{
                                if($d==0){
                                    $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where child_id = '".$getObjectIDCurrentApprover['object_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                                }else{
                                    $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where (child_id = '".$masterOrgChart['master_id']."' || child_id='".$masterOrgChart['object_id']."') and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                }
                                $queryMasterNodeOrgChart = $db->query("SELECT * FROM tborgchartobjects where object_id='".$masterOrgChart['master_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                $dept = $queryMasterNodeOrgChart['role'];
                            }
                            //echo $queryMasterNode['role']."_".$dept."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                        }else{
                            $dept = "";
                            //echo $queryMasterNode['role']."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                        }
                    }
                    //echo $queryMasterNode['role'].$dept."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                    array_push($arrayWorkflowRole,$queryMasterNode['role']);
                    array_push($arrayWorkflowRoleStatus,$queryMasterNode['status']);
                    array_push($arrayWorkflowObjID,$master['child_id']);
                    array_push($arrayOrgchart,$dept);
                    $d++;
                }
                $minus = 0;
                //temporary get the flow of the approver
                $roleToInserts = "";$object_id2 = "";$lastApproverCount=0;$getRowsApprover = 0;$confirmation=0;$confirm = 0;$proceedToNext = 0;$approverFlow = "";
                for($o=0;$o<count($arrayWorkflowRole);$o++){
                    if($arrayWorkflowRole[$o]=="Head" || $arrayWorkflowRole[$o]=="Assistant Head"){
                        if($arrayWorkflowRole[$o]=="Head" and $arrayWorkflowRole[$o-1]=="Assistant Head"){
                            $minus++;
                        }
                        $roleToInserts = $arrayWorkflowRole[$o]."_".$arrayOrgchart[$o-$minus];
                        $getRowsApprover = $db->query("SELECT * FROM tbuser where role_type='".tempGroupType($arrayWorkflowRole[$o])."' and role='".$arrayOrgchart[$o-$minus]."' and is_active=1 and status =1 and company_id = '$company_id' and id!=$userID","numrows");
                        //echo $roleToInserts."->".$getRowsApprover."\n";
                        if($getRowsApprover>0){
                            $lastApproverCount++;
                            $approver = $arrayWorkflowRole[$o]."_".$arrayOrgchart[$o-$minus];
                        }
                    }else{
                        $roleToInserts = $arrayWorkflowRole[$o];
                        $getRowsApprover = $db->query("SELECT * FROM tbuser u LEFT JOIN tbposition p on u.position_id = p.id where (concat_ws(' ',u.firstname,u.lastname) = '".$roleToInserts."' or p.position = '".$roleToInserts."') and u.is_active=1 and u.status =1 and u.company_id = $company_id and u.id!=$userID","numrows");
                        //echo $roleToInserts."->".$getRowsApprover."\n";
                        if($getRowsApprover>0){
                           $approver = $arrayWorkflowRole[$o];
                           $lastApproverCount++;
                        }
                    }
                    if($o==0 && $getRowsApprover==0){
                        $confirm = 1;
                    }
                    if($getRowsApprover>0){
                        $approverStatus = $arrayWorkflowRoleStatus[$o];
                        $master_id = $arrayWorkflowObjID[$o];
                        $proceedToNext++;
                        //echo $request_id[$a]." ".$approver."\n";
                        break;
                    }
                }
                /*$getRequestor = $db->query("SELECT * FROM tbuser where id='".$userID."'","row");
                $getObjectID = $db->query("SELECT object_id from tborgchartobjects where role='".$getRequestor['role']."' and orgChart_id='".$getActiveOrgChart['id']."'","row");
                $d = 0;
                $lastApproverCount = 0;
                while(true){
                    if($d==0){
                        $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = '$nextAproverID[$a]' and worflow_id = '$diagram_id[$a]'","row");    
                    }else{
                        $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = '".$master['child_id']."' and worflow_id = '$diagram_id[$a]'","row");    
                    }
                    $queryMasterNode = $db->query("SELECT * FROM tbworflow_obects where object_id='".$master['child_id']."' and worflow_id = '$diagram_id[$a]'","row");
                    if($queryMasterNode['role']=="END"){
                        break;
                    }else{
                        if($queryMasterNode['role']=="Head" || $queryMasterNode['role']=="Assistant Head"){
                            //FOR HEAD OR ASSISTANT
                            if($d==0 && $getRequestor['role_type']==2 && $queryMasterNode['role']=="Head"){
                                $userRole = $getRequestor['role'];
                                $approver =  $queryMasterNode['role']."_".$getRequestor['role'];
                                $masterOrgChart = $db->query("SELECT object_id FROM tborgchartobjects where role='".$getRequestor['role']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                            }else{
                                if($d==0){
                                    $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where child_id = '".$getObjectID['object_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                                }else{
                                    $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where (child_id = '".$masterOrgChart['master_id']."' || child_id='".$masterOrgChart['object_id']."') and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                                }
                                $queryMasterNodeOrgChart = $db->query("SELECT * FROM tborgchartobjects where object_id='".$masterOrgChart['master_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                $approver =  $queryMasterNode['role']."_".$queryMasterNodeOrgChart['role'];
                                $userRole = $queryMasterNodeOrgChart['role'];
                            }
                            //echo "SELECT * FROM tbuser where role_type='".tempGroupType($queryMasterNode['role'])."' and role='$userRole' and is_active=1 and status =1 and id!=$userID";
                            $getRowsApprover = $db->query("SELECT * FROM tbuser where role_type='".tempGroupType($queryMasterNode['role'])."' and role='$userRole' and is_active=1 and status =1 and id!=$userID and company_id=$company_id","numrows");
                            if($getRowsApprover==1){
                                $lastApproverCount++;
                            }
                        }else{
                            //FOR SPECIFIC USER OR POSITION
                            $getRowsApprover = $db->query("SELECT * FROM tbuser u LEFT JOIN tbposition p on u.position_id = p.id where concat_ws(' ',u.firstname,u.lastname) = '".$queryMasterNode['role']."' or p.position = '".$queryMasterNode['role']."' and u.is_active=1 and u.status =1 and u.id!=$userID and u.company_id=$company_id","numrows");
                            if($getRowsApprover>0){
                                $approver = $queryMasterNode['role'];
                                $lastApproverCount++;
                            }
                        }
                        if($lastApproverCount>0){
                            $master_id = $queryMasterNode['object_id'];
                            $approverStatus = $queryMasterNode['status'];
                            //echo $approver."->".$master_id."->".$approverStatus;
                            break;
                        }
                    }
                    $d++;
                }*/
                //balik sa umpisa
                if($lastApproverCount>0){
                    //for($newReq=0;$newReq<count($request_id);$newReq++){
                        
                    //}
                    $deleteOldReq = array(
                        "request_id"=>$request_id[$a]
                        );
                    $db->delete("tbrequest_process",$deleteOldReq);
                    $insertRequestProcess = array(
                                "request_id"=>$request_id[$a],
                                "approver"=>$approver,
                                "approverStatus"=>$approverStatus,
                                "wf_master_id"=>$master_id,
                                "status"=>0
                        );
                    $requestProcess_id = $db->insert("tbrequest_process",$insertRequestProcess);
                }else{
                    $getRequestor = $db->query("SELECT * FROM tbuser where id='".$requestor[$a]."'","row");
                    $user_group = $getRequestor['role'];
                    $user_groupType = $getRequestor['role_type'];
                    $getObjectID = $db->query("SELECT object_id from tborgchartobjects where role='$user_group' and orgChart_id='".$getActiveOrgChart['id']."'","row");
                    $d = 0;
                    $arrayWorkflowRole = array();
                    $arrayWorkflowRoleStatus = array();
                    $arrayOrgchart = array();
                    $arrayWorkflowObjID = array();
                    $push = false;
                    while(true){
                        if($d==0){
                            $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = 'flowspan_1_worklow' and worflow_id = '$diagram_id[$a]'","row");    
                        }else{
                            $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = '".$master['child_id']."' and worflow_id = '$diagram_id[$a]'","row");    
                        }
                        $queryMasterNode = $db->query("SELECT * FROM tbworflow_obects where object_id='".$master['child_id']."' and worflow_id = '$diagram_id[$a]'","row");
                        if($queryMasterNode['role']=="END"){
                            break;
                        }else{
                            //echo $queryMasterNode['role']."\n";
                            if($queryMasterNode['role']=="Head" || $queryMasterNode['role']=="Assistant Head"){
                                //for the the submitter
                                if($d==0 and $user_groupType!=1){ //hindi head
                                    $dept = $user_group;
                                    $masterOrgChart = $db->query("SELECT object_id FROM tborgchartobjects where role='$user_group' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                }else{
                                    if($d==0){
                                        $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where child_id = '".$getObjectID['object_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                                    }else{
                                        $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where (child_id = '".$masterOrgChart['master_id']."' || child_id='".$masterOrgChart['object_id']."') and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                    }
                                    $queryMasterNodeOrgChart = $db->query("SELECT * FROM tborgchartobjects where object_id='".$masterOrgChart['master_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                    $dept = $queryMasterNodeOrgChart['role'];
                                    
                                }
                                //echo $queryMasterNode['role']."_".$dept."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                            }else{
                                $dept = "";
                                //echo $queryMasterNode['role']."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                            }
                        }
                        //echo $queryMasterNode['role'].$dept."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                        array_push($arrayWorkflowRole,$queryMasterNode['role']);
                        array_push($arrayWorkflowRoleStatus,$queryMasterNode['status']);
                        array_push($arrayWorkflowObjID,$master['child_id']);
                        array_push($arrayOrgchart,$dept);
                        $d++;
                    }
                    $minus = 0;
                    //temporary get the flow of the approver
                    $roleToInserts = "";$object_id2 = "";$lastApproverCount=0;$getRowsApprover = 0;$confirmation=0;$confirm = 0;$proceedToNext = 0;$approverFlow = "";
                    for($o=0;$o<count($arrayWorkflowRole);$o++){
                        if($arrayWorkflowRole[$o]=="Head" || $arrayWorkflowRole[$o]=="Assistant Head"){
                            if($arrayWorkflowRole[$o]=="Head" and $arrayWorkflowRole[$o-1]=="Assistant Head"){
                                $minus++;
                            }
                            $roleToInserts = $arrayWorkflowRole[$o]."_".$arrayOrgchart[$o-$minus];
                            $getRowsApprover = $db->query("SELECT * FROM tbuser where role_type='".tempGroupType($arrayWorkflowRole[$o])."' and role='".$arrayOrgchart[$o-$minus]."' and is_active=1 and status =1 and company_id = '$company_id' and id!=$userID","numrows");
                            //echo $roleToInserts."->".$getRowsApprover."\n";
                            if($getRowsApprover>0){
                                $lastApproverCount++;
                                $approver = $arrayWorkflowRole[$o]."_".$arrayOrgchart[$o-$minus];
                            }
                        }else{
                            $roleToInserts = $arrayWorkflowRole[$o];
                            $getRowsApprover = $db->query("SELECT * FROM tbuser u LEFT JOIN tbposition p on u.position_id = p.id where (concat_ws(' ',u.firstname,u.lastname) = '".$roleToInserts."' or p.position = '".$roleToInserts."') and u.is_active=1 and u.status =1 and u.company_id = $company_id and u.id!=$userID","numrows");
                            //echo $roleToInserts."->".$getRowsApprover."\n";
                            if($getRowsApprover>0){
                               $approver = $arrayWorkflowRole[$o];
                               $lastApproverCount++;
                            }
                        }
                        if($o==0 && $getRowsApprover==0){
                            $confirm = 1;
                        }
                        if($getRowsApprover>0){
                            $approverStatus = $arrayWorkflowRoleStatus[$o];
                            $master_id = $arrayWorkflowObjID[$o];
                            $proceedToNext++;
                            //echo $request_id[$a]." ".$approver."\n";
                            break;
                        }
                    }
                    /*
                    $checkApprover = 0;
                    $getRequestor = $db->query("SELECT * FROM tbuser where id='".$requestor[$a]."'","row");
                    $getObjectID = $db->query("SELECT object_id from tborgchartobjects where role='".$getRequestor['role']."' and orgChart_id='".$getActiveOrgChart['id']."'","row");
                    $d = 0;
                    while(true){
                        if($d==0){
                            $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = 'flowspan_1_worklow' and worflow_id = '$diagram_id[$a]'","row");    
                        }else{
                            $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = '".$master['child_id']."' and worflow_id = '$diagram_id[$a]'","row");    
                        }
                        $queryMasterNode = $db->query("SELECT * FROM tbworflow_obects where object_id='".$master['child_id']."' and worflow_id = '$diagram_id[$a]'","row");
                        if($queryMasterNode['role']=="END"){
                            break;
                        }else{
                            if($queryMasterNode['role']=="Head" || $queryMasterNode['role']=="Assistant Head"){
                                //FOR HEAD OR ASSISTANT
                                if($d==0 && $getRequestor['role_type']!=1 && $queryMasterNode['role']=="Head"){
                                    $userRole = $getRequestor['role'];
                                    $approver =  $queryMasterNode['role']."_".$getRequestor['role'];
                                    $masterOrgChart = $db->query("SELECT object_id FROM tborgchartobjects where role='".$getRequestor['role']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                }else{
                                    if($d==0){
                                        $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where child_id = '".$getObjectID['object_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                                    }else{
                                        $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where (child_id = '".$masterOrgChart['master_id']."' || child_id='".$masterOrgChart['object_id']."') and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                                    }
                                    $queryMasterNodeOrgChart = $db->query("SELECT * FROM tborgchartobjects where object_id='".$masterOrgChart['master_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                    $approver =  $queryMasterNode['role']."_".$queryMasterNodeOrgChart['role'];
                                    $userRole = $queryMasterNodeOrgChart['role'];
                                }
                                $getRowsApprover = $db->query("SELECT * FROM tbuser where role_type='".tempGroupType($queryMasterNode['role'])."' and role='$userRole' and is_active=1 and status =1 and id!=$userID","numrows");
                                if($getRowsApprover==1){
                                    $checkApprover++;
                                }
                            }else{
                                //FOR SPECIFIC USER OR POSITION
                                $getRowsApprover = $db->query("SELECT * FROM tbuser u LEFT JOIN tbposition p on u.position_id = p.id where concat_ws(' ',u.firstname,u.lastname) = '".$queryMasterNode['role']."' or p.position = '".$queryMasterNode['role']."' and u.is_active=1 and u.status =1 and u.id!=$userID","numrows");
                                if($getRowsApprover>0){
                                    $approver = $queryMasterNode['role'];
                                    $checkApprover++;
                                }
                            }
                            if($checkApprover>0){
                                $master_id = $queryMasterNode['object_id'];
                                $approverStatus = $queryMasterNode['status'];
                                //echo $approver."->".$master_id."->".$approverStatus;
                                break;
                            }
                        }
                        $d++;
                    }*/
                    if($checkApprover==0){
                        echo "wala na tlga";
                    }else{
                        //for($newReq=0;$newReq<count($request_id);$newReq++){
                            
                        //}
                        $deleteOldReq = array(
                            "request_id"=>$request_id[$a]
                            );
                        $db->delete("tbrequest_process",$deleteOldReq);
                        $insertRequestProcess = array(
                                    "request_id"=>$request_id[$a],
                                    "approver"=>$approver,
                                    "approverStatus"=>$approverStatus,
                                    "wf_master_id"=>$master_id,
                                    "status"=>0
                            );
                        $requestProcess_id = $db->insert("tbrequest_process",$insertRequestProcess);
                    }
                    
                }
            }else{
                echo "Error on Config Setting";
            }
        }
    }else{
        //echo "No Request to Approver Found";
    }
}
?>